﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class CongNo : DevExpress.XtraEditors.XtraForm
    {
        String sErr = "";
        DataRow _row = null;
        string loaithem = "";
        int idThem = 41;
        int idXoa = 43;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThemNG.Visible = btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                //btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
            }
        }
        public CongNo()
        {
            InitializeComponent();
        }

        private void rgLoaiCT_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCongNo();
        }

        private void LoadCongNo()
        {
            BUS.CONGNOControl ctr = new BUS.CONGNOControl();
            DataTable dt = ctr.GetALLCongNo(null, rgLoaiCT.EditValue.ToString(), frmMain.__ngayMoSo, ref sErr);
            gridCongNo.DataSource = dt;
        }

        private void RefresForm(string p)
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            //DataTable dt = ctr.GetALLByCT(null, null, frmMain.__ngayMoSo, ref sErr);
            //int i = 1;
            //string soCT = (dt.Rows.Count + i).ToString("00000");


            //while (ctr.IsExist(soCT, null, frmMain.__ngayMoSo))
            //{
            //    soCT = (dt.Rows.Count + i).ToString("00000");
            //    i++;
            //}
            //txtCT.Text = soCT;
            BUS.CommonControl cmm = new BUS.CommonControl();
            txtCT.Text = cmm.GetSoCTNextNo(frmMain.__ngayMoSo.ToString("yyyy-MM-dd"));

            txtTenKH.EditValue = p;
            txtTenKH.Tag = null;
            //      txtDienThoai.EditValue = p;
            txtDiaChi.EditValue = p;
            txtCMND.EditValue = p;
            txtNgayCap.EditValue = null;
            txtNoiCap.EditValue = p;
            txtSoTien.EditValue = p;
            txtNoiDung.EditValue = p;
            //txtCT.EditValue = p;
            txtNgayGD.EditValue = p;
            txtTT.EditValue = p;
            txtTKNo.EditValue = p;
            txtTKCo.EditValue = p;
            txtTaiKhoan.EditValue = p;
            txtNganHang.EditValue = p;
            txtTP.EditValue = p;
            txtNgayGD.EditValue = frmMain.__ngayMoSo;
            txtSoTien.EditValue = p;
        }

        private void EnabledForm(bool p)
        {


            //     rgLoaiCT.Enabled = p;
            //txtTenKH.Properties.ReadOnly = !p;
            //        txtDienThoai.Properties.ReadOnly = !p;
            //txtDiaChi.Properties.ReadOnly = !p;
            txtCMND.Properties.ReadOnly = !p;
            txtNgayCap.Properties.ReadOnly = !p;
            txtNoiCap.Properties.ReadOnly = !p;
            txtSoTien.Properties.ReadOnly = !p;
            txtNoiDung.Properties.ReadOnly = !p;
            //txtCT.Properties.ReadOnly = true;
            //txtNgayGD.Properties.ReadOnly = true;
            //txtTT.Properties.ReadOnly = true;
            //txtTKNo.Properties.ReadOnly = true;
            //txtTKCo.Properties.ReadOnly = true;
            txtTaiKhoan.Properties.ReadOnly = !p;
            txtNganHang.Properties.ReadOnly = !p;
            txtTP.Properties.ReadOnly = !p;

            if (frmMain.__khoaSo)
            {
                btnXoa.Enabled = btnThemNG.Enabled = btnThem.Enabled = false;
            }
            else
            {
                btnXoa.Enabled = btnThemNG.Enabled = btnThem.Enabled = !p;
            }

            btnLuu.Enabled = p;
            btnBoQua.Enabled = p;

            btnTimKiem.Enabled = !p;
            btnInCTCN.Enabled = btnIn.Enabled = !p;

        }

        private void CongNo_Load(object sender, EventArgs e)
        {
            rgLoaiCT.SelectedIndex = 0;
            EnabledForm(false);
            RefresForm("");
            SetPermission();
            LoadCongNo();
        }



        private void LoadCongNoInf()
        {
            _row = ((DataRowView)gridCongNo.CurrentRow.DataRow).Row;

            BUS.CUSTINFOControl custctr = new BUS.CUSTINFOControl();
            DTO.CUSTINFOInfo custinf = custctr.Get(_row["MaKH"].ToString(), ref sErr);

            txtTenKH.EditValue =_row["TENGD"].ToString();
            txtDiaChi.EditValue = _row["DIACHIKH"].ToString();
            if (loaithem == "CK")
            {
                txtTaiKhoan.EditValue = custinf.TAIKHOAN;
                txtNganHang.EditValue = custinf.NGANHANG;
                txtTP.EditValue = custinf.TINH_TP;
            }
            //    txtCMND.EditValue = custinf.
            txtSoTien.EditValue = _row["SoTien"];
            txtNoiDung.EditValue = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(true);

            txtTaiKhoan.Properties.ReadOnly = txtNganHang.Properties.ReadOnly = txtTP.Properties.ReadOnly = true;

            txtTT.EditValue = "Normal";
            LoadCongNoInf();
            txtTenKH.Focus();
            loaithem = "TM";
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (Convert.ToDecimal(txtSoTien.EditValue) <= 0)
            {
                XtraMessageBox.Show("Vui lòng nhập số tiền trả", "Thông báo");
                return;
            }
            sErr = SaveGiaoDich();
            if (sErr == "")
            {

                sErr = SaveCongNo();

            }
            if (sErr != "")
                XtraMessageBox.Show(sErr);
            else
            {
                XtraMessageBox.Show("Lưu thành công");
                //      RefresForm("");
                EnabledForm(false);
                LoadCongNo();
            }
        }

        protected String SaveGiaoDich()
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            DTO.GIAODICHInfo inf = new DTO.GIAODICHInfo();

            //DataTable dt = ctr.GetALLByCT(null, null, frmMain.__ngayMoSo, ref sErr);
            //int i = 1;
            //txtCT.Text = (dt.Rows.Count + i).ToString("00000");

            //while (ctr.IsExist(txtCT.Text, null, frmMain.__ngayMoSo))
            //{
            //    txtCT.Text = (dt.Rows.Count + i).ToString("00000");
            //    i++;
            //}

            BUS.CommonControl cmm = new BUS.CommonControl();
            txtCT.Text = cmm.GetSoCTNextNo(frmMain.__ngayMoSo.ToString("yyyy-MM-dd"));

            inf.SOCT = txtCT.Text;
            if (rgLoaiCT.EditValue == "BH")
                inf.LOAICT = "UNC01";
            else
                inf.LOAICT = "UNC02";
            inf.NGAYCT = (DateTime)txtNgayGD.EditValue;
            inf.MAKH = _row["MAKH"].ToString();
            //     inf.TIENHANG = Convert.ToDouble(txtTienHang.EditValue);
            //      inf.PHIVC = Convert.ToDouble(txtPhiVanChuyen.EditValue);
            //     inf.CHIETKHAU = Convert.ToDouble(txtChieutkhau.EditValue);
            inf.TONGTIEN = Convert.ToDouble(txtSoTien.EditValue);
            //       inf.CONGNO = Convert.ToDouble(txtTienThieu.EditValue);
            //      inf.THUNGAY = Convert.ToDouble(txtSoTien.EditValue);
            inf.TIENMAT = Convert.ToDouble(txtSoTien.EditValue);
            inf.LOAITIEN = "VND";
            inf.NameKH = txtTenKH.EditValue.ToString();
            inf.DIACHIKH = txtDiaChi.Text;
            //      inf.DIENTHOAI = txtDienThoai.Text;
            inf.TAIKHOAN = txtTaiKhoan.EditValue.ToString();
            inf.NGANHANG = txtNganHang.EditValue.ToString();
            inf.TINH_TP = txtTP.EditValue.ToString();

            inf.NOIDUNG = txtNoiDung.Text;
            inf.TINHTRANG = txtTT.Text;
            inf.INHD = true;
            inf.CREATEBY = frmMain.__userID;
            inf.MODIFIEDBY = frmMain.__userID;
            inf.CREATEDATE = DateTime.Today;
            inf.MODIFIEDDATE = DateTime.Today;

            ctr.Add(inf, ref sErr);

            return sErr;
        }
        protected String SaveCongNo()
        {
            if (txtCT.EditValue.ToString() != "")
            {
                BUS.CONGNOControl ctr = new BUS.CONGNOControl();
                DTO.CONGNOInfo congnoinf = new DTO.CONGNOInfo();

                congnoinf.SOCT = txtCT.Text;
                congnoinf.NGAYCT = (DateTime)txtNgayGD.EditValue;
                congnoinf.DIACHIKH = txtDiaChi.Text;
                //      congnoinf.DIENTHOAI = ;
                if (rgLoaiCT.EditValue == "BH")
                {
                    congnoinf.PHAITHU = false;
                    congnoinf.D_C = "C";
                    congnoinf.NOIDUNG = "Khách hàng trả trước";
                    congnoinf.SOTIEN = Convert.ToDouble(txtSoTien.EditValue) * (-1);
                }
                else
                {
                    congnoinf.PHAITHU = true;
                    congnoinf.D_C = "C";
                    congnoinf.NOIDUNG = "Trả tiền nhập kho";
                    congnoinf.SOTIEN = Convert.ToDouble(txtSoTien.EditValue);
                }


                congnoinf.MAKH = _row["MAKH"].ToString();
                congnoinf.SOCTTT = rgLoaiCT.EditValue.ToString();
                congnoinf.CREATEBY = frmMain.__userID;
                congnoinf.MODIFIEDBY = frmMain.__userID;
                congnoinf.CREATEDATE = DateTime.Today;
                congnoinf.MODIFIEDDATE = DateTime.Today;

                ctr.Add(congnoinf, ref sErr);


            }

            return sErr;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtTenKH.Tag != null)
            {
                if (XtraMessageBox.Show("Bạn có thực sự muốn xóa công nợ ngày", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    if (Convert.ToDateTime(txtNgayGD.EditValue) != frmMain.__ngayMoSo)
                    {
                        if (XtraMessageBox.Show("Ngày công nợ khác ngày mở sổ, bạn có chắc muốn xóa công nợ ngày", "Thông báo", MessageBoxButtons.OKCancel) != DialogResult.OK)
                            return;
                        DeleteCongNo();
                    }
                    else
                    {
                        DeleteCongNo();
                    }
                }

            }
        }

        private void DeleteCongNo()
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            string loaict = "";
            if (rgLoaiCT.EditValue == "BH")
                loaict = "UNC01";
            else
                loaict = "UNC02";
            sErr = ctr.Delete(txtCT.Text, loaict, Convert.ToDateTime(txtNgayGD.EditValue));
            if (sErr == "")
            {
                BUS.CONGNOControl cnCtr = new BUS.CONGNOControl();
                sErr = cnCtr.DeleteCN(txtCT.Text, rgLoaiCT.EditValue.ToString(), Convert.ToDateTime(txtNgayGD.EditValue));
            }
            if (sErr != "")
                XtraMessageBox.Show(sErr);
            else
            {
                RefresForm("");
                XtraMessageBox.Show("Xóa công nợ thành công", "Thông báo");
                EnabledForm(false);
                LoadCongNo();
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            Windows.frmViewGiaoDich gd = new Windows.frmViewGiaoDich();
            if (rgLoaiCT.EditValue == "BH")
                gd.LoaiCT = "UNC01";
            else
                gd.LoaiCT = "UNC02";
            if (gd.ShowDialog(this) == DialogResult.OK)
            {
                DTO.GIAODICHInfo inf = new DTO.GIAODICHInfo(gd.ReturnRow);

                BUS.CUSTINFOControl cusctr = new BUS.CUSTINFOControl();
                DTO.CUSTINFOInfo cusinf = cusctr.Get(inf.MAKH, ref sErr);
                txtTenKH.Tag = cusinf.CUSTID;
                txtTenKH.Text = cusinf.TENGD;
                txtCT.Text = inf.SOCT;
                txtNgayGD.EditValue = inf.NGAYCT;
                //      IDKH = inf.MAKH;
                //        txtSoTien.EditValue = inf.TIENHANG;
                ////        txtPhiVanChuyen.EditValue = inf.PHIVC;
                //       txtChieutkhau.EditValue = inf.CHIETKHAU;
                txtSoTien.EditValue = inf.TONGTIEN;
                //        txtTienThieu.EditValue = inf.CONGNO;
                //        txtTienDatTruoc.EditValue = inf.THUNGAY;

                txtDiaChi.Text = inf.DIACHIKH;
                txtTaiKhoan.Text = inf.TAIKHOAN;
                txtNganHang.EditValue = inf.NGANHANG;
                txtTP.EditValue = inf.TINH_TP;
                txtNoiDung.EditValue = inf.NOIDUNG;
                txtTT.EditValue = inf.TINHTRANG;


                EnabledForm(false);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnThemNG_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(true);
            LoadCongNoInf();
            txtTT.EditValue = "Normal";
            txtTenKH.Focus();
            loaithem = "CK";
        }

        private void gridView_Click(object sender, EventArgs e)
        {
            if (gridCongNo.CurrentRow != null)
            {
                if (btnThem.Enabled == false)
                {
                    LoadCongNoInf();
                    txtTenKH.Focus();
                }
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            frmMain main = this.Parent.Parent as frmMain;

            if (txtCT.Text != "" && txtNgayGD.Text != "")
            {

                string loaict = rgLoaiCT.EditValue.ToString() == "BH" ? "UNC01" : "UNC02";
                DataTable dt = ctr.GetALLByCT(txtCT.Text, loaict, Convert.ToDateTime(txtNgayGD.EditValue), ref sErr);
                if (loaict == "UNC01")
                {

                    main.ShowReport(dt.DataSet, "PTTM", "Phiếu thu tiền mặt");

                }
                else
                {
                    if (txtTaiKhoan.Text == "" && txtNganHang.Text == "")
                        main.ShowReport(dt.DataSet, "PCTM", "Phiếu chi tiền mặt");
                    else
                        main.ShowReport(dt.DataSet, "PC_CK", "Phiếu chi chuyển khoản");
                }
            }
        }

        private void btnInCTCN_Click(object sender, EventArgs e)
        {
            if (gridCongNo.CurrentRow != null)
            {
                _row = ((DataRowView)gridCongNo.CurrentRow.DataRow).Row;
                if (_row != null)
                {
                    BUS.CUSTINFOControl custctr = new BUS.CUSTINFOControl();
                    DTO.CUSTINFOInfo custinf = custctr.Get(_row["MaKH"].ToString(), ref sErr);


                    Windows.Report.frmBaoCaoCTCNKH frm = new Windows.Report.frmBaoCaoCTCNKH();
                    frm.Tenkh = custinf.TENGD;
                    frm.Makh = custinf.CUSTID;
                    if (custinf.NHACUNGCAP) frm.Loaict = "NH";
                    else frm.Loaict = "BH";
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        frmMain main = (frmMain)this.Parent.Parent;
                        main.ShowReport(frm.DataSource, "BCCTCNKH", frm.Text, frm.ValueList);
                    }
                }
            }
        }

        private void txtSoTien_EditValueChanged(object sender, EventArgs e)
        {
            if (Convert.ToDouble(txtSoTien.EditValue) < 0)
                txtSoTien.EditValue = 0;
        }

        private void CongNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
               
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
               
            }
        }
    }
}